<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Request;

class Config extends Backend
{
    //构造函数
    public function __construct()
    {
        parent::__construct();

        //使用config模型
        $this->ConfigModel = model('Config');
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        //查询总数
        $count = $this->ConfigModel->count();
        $config = $this->ConfigModel->paginate(10);

        $this->assign([
            'count' => $count,
            'config' => $config,
        ]);
        return $this->fetch();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $config = $this->request->param();

            //封装一个新的数组
            $data = [
                'title' => $config['title'],
                'key' => $config['key'],
                'type' => $config['type'],
            ];

            //判断配置类型
            if ($config['type'] == 'text') {
                $data['value'] = $config['value'];
            } elseif ($config['type'] == 'file') {
                //处理上传
                $file = request()->file('file');

                //不为空
                if ($file) {
                    //路径
                    $path = ROOT_PATH . 'public' . DS . 'uploads';

                    //把图片移动到指定目录里同时验证格式以及时间名
                    $info = $file->validate(['ext' => 'jpg,png,gif,jpeg'])->rule('date')->move($path);
                    if ($info) {
                        $data['value'] = DS . 'uploads' . DS . $info->getSaveName();
                    }
                }
            }


            $result = $this->ConfigModel->validate('common/Config')->save($data);

            if ($result === FALSE) {
                $this->error($this->ConfigModel->getError());
                exit;
            } else {
                $this->success('添加成功', url('admin/config/list'));
                exit;
            }
        }

        return $this->fetch();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //通过id从数据表里面查询该条数据
        $config = $this->ConfigModel->find($id);

        //该数据是否存在
        if (!$config) {
            $this->error('该条数据不存在,请重新选择');
            exit;
        }

        if ($this->request->isPost()) {
            //id
            $id = $this->request->param('id', 0);
            //标题
            $title = $this->request->param('title', '');
            //配置的值
            $value = $this->request->param('value', '');
            //类型
            $type = $this->request->param('type', '');

            //封装一个新的数据
            $data = [
                'id' => $id,
                'title' => $title,
            ];

            //判断类型
            if ($type == 'text') {
                $data['value'] = $value;
            } elseif ($type == 'file') {
                if ($_FILES['file']['size'] > 0) {
                    $file = upload('file');

                    if ($file === FALSE) {
                        $this->error('上传失败');
                        exit;
                    }

                    $data['value'] = $file;
                }
            }

            //更新数据库
            $request = $this->ConfigModel->isUpdate(true)->save($data);

            if ($request === FALSE) {
                $this->error($this->ConfigModel->getError());
                exit;
            } else {
                //判断提交的类型是不是file并且判断file的文件大小大于0
                if ($config['type'] == 'file' && $_FILES['file']['size'] > 0) {
                    //获取旧图片名称并把最左边的/去掉
                    $old = ltrim($config['value'], '/');

                    //检查文件是否存在并且把它删除
                    @is_file($old) && @unlink($old);
                }
                $this->success('修改成功', url('admin/config/list'));
                exit;
            }
        }

        //模板赋值
        $this->assign([
            'config' => $config
        ]);

        return $this->fetch();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //接收id
        if ($this->request->isPost()) {
            //如果有传过来id
            if ($this->request->param('id')) {
                $id = $this->request->param('id');
                $this->ConfigModel->destroy($id);
                $this->finish('删除成功');
            } else {
                $id = $this->request->param('ids');
                $this->warning('删除失败,id异常');
            }
        }
    }
}
